﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.TextUtils;
using CatUtils.Utils.SpecialUtils.Html;
using CatUtilsDll.Utils.Currency.WebUtils;
using RabbitDownload.Helper.Currency;
using System;
using System.Collections.Generic;

namespace RabbitDownload.WebSite.MoeRabbit.MoeRAll
{
    internal class MoeAllScanner
    {
        public static void Start(string StartPage, string EndPage)
        {
            int StartCount = TextHelper.StringToInt(StartPage);
            int EndCount = TextHelper.StringToInt(EndPage);
            List<string> AllImglist = new List<string>();
            if (EndCount < StartCount || StartCount == 0)
            {
                Console.WriteLine("起始页小于结束页");
                return;
            }
            AllImglist.AddRange(ReadContent(StartCount, EndCount));
            WebSite.WebCurrency.SaveHelper.AutoSave(AllImglist, "萌图兔", "全部");
        }

        /// <summary>
        /// 解析并且获取文章列表
        /// </summary>
        /// <param name="startCount"></param>
        /// <param name="endCount"></param>
        /// <returns></returns>
        private static List<string> ReadContent(int startCount, int endCount)
        {
            List<string> AllImg = new List<string>();

            for (int page = startCount; page <= endCount; page++)
            {
                String Shtml = WebHelper.HttpPcGet("http://pic.moetutu.com/page/" + page);
                Console.WriteLine("第{0}页正在扫描", page);
                List<string> ArticleTemp = new List<string>();

                foreach (string line in HtmlHelper.GetLinkVlaueList(Shtml, "//a[@class='thumb-holder-link']"))
                {
                    if (MoeRabbitStaticValue.OldUrlList.Contains(line))
                    {
                        Console.WriteLine("已有，跳过扫描");
                        continue;
                    }
                    ArticleTemp.Add(line);
                }
                if (ArticleTemp.Count > 0)
                {
                    AllImg.AddRange(AnalysisContent(ArticleTemp));
                    Console.WriteLine("已扫描{0}张", AllImg.Count);
                }
                else
                {
                    Console.WriteLine("没有需要扫描的文章！");
                    break;
                }
            }
            return AllImg;
        }

        /// <summary>
        /// 分析文章，获取图片
        /// </summary>
        /// <param name="ArticleTemp"></param>
        /// <returns></returns>
        private static List<string> AnalysisContent(List<string> ArticleTemp)
        {
            List<string> AllImg = new List<string>();
            foreach (string ArticleURL in ArticleTemp)
            {
                WebCurrency.WebSiteHelper.RefreshLog(ref MoeRabbitStaticValue.OldUrlList, ArticleURL, MoeRabbitStaticValue.MoeLogPath1);
                Console.WriteLine("日志数量" + MoeRabbitStaticValue.OldUrlList.Count);
                Console.WriteLine(ArticleURL);
                try
                {
                    String ArticleShtml = WebHelper.HttpPcGet(ArticleURL);
                    String ImgLink = HtmlHelper.GetFirstLink(ArticleShtml, "//a[@class='Original_img_url']");
                    if (ImgLink == "")
                        continue;
                    if (ImgLink.IndexOf("pximg.net") > 0)
                    {
                        Console.WriteLine("P站图片");
                        if (new WebHelper().GetStatusMove(ImgLink))
                        {
                            Console.WriteLine("图片已失效");
                            continue;
                        }
                    }
                    AllImg.Add(ImgLink);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }
            return AllImg;
        }
    }
}